<template>
  <div class="softWorksDetails-container-padding">
    <div class="buttonList">
      <el-button type="primary" @click="edit">编辑软著信息</el-button>
      <el-button type="danger" @click="del">删除</el-button>
    </div>
    <el-descriptions title="">
      <el-descriptions-item v-for="item in softWorksDescriptions" :key="item.label" :label="item.label">
        {{ formInline[item.value] }}
      </el-descriptions-item>
    </el-descriptions>
    <el-divider />
    <div class="title">操作日志</div>
    <vxe-grid v-bind="operationLog" :height="height">
      <template #pager>
        <!--使用 pager 插槽-->
        <vxe-pager v-model:current-page="tablePage.currentPage" v-model:page-size="tablePage.pageSize" :layouts="[
          'Sizes',
          'PrevJump',
          'PrevPage',
          'Number',
          'NextPage',
          'NextJump',
          'FullJump',
          'Total',
        ]" :total="tablePage.total" @page-change="handlePageChange" />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
import { VxePagerEvents } from 'vxe-table';
import { softWorksDescriptions, operationLog } from '../table/column'
import { reactive, onMounted } from 'vue'
import { getSoftwareInfo, deleteSoftware } from '@/api/property'
import { useCloseTabNavigate } from '@/utils/useCloseTabNavigate'

const formInline = ref({})
const route = useRoute()
const router = useRouter()
const id = route.query.id ?? undefined
const { closeTabNavigate } = useCloseTabNavigate()
onMounted(() => {
  if (id) {
    getSoftwareInfo({ id: id , getType: 2}).then((res) => {
      formInline.value = res.data
    })
  }
})

const edit = () => {
  // isEdit.value = true
  router.push({ path: '/property/softWorks/add', query: { id: id } })
}

const del = () => {
  ElMessageBox.confirm('确定删除该条数据?', '删除', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    let params = Number(id)
    deleteSoftware(params).then((res) => {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      closeTabNavigate({ name: 'SoftWorks' })
    })
  })
}

const tablePage = reactive({
  total: 0,
  currentPage: 1,
  pageSize: 10,
})

const handlePageChange: VxePagerEvents.PageChange = ({
  currentPage,
  pageSize,
}) => {
  tablePage.currentPage = currentPage
  tablePage.pageSize = pageSize
  // findList()
}

const height = ref(0)
interface BaseTableHeight {
  (padding: number): number;
}
const $baseTableHeight = inject<BaseTableHeight>('$baseTableHeight')
onBeforeMount(() => {
  window.addEventListener('resize', handleHeight)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleHeight)
})

const handleHeight = () => {
  height.value = $baseTableHeight(0)
}
</script>

<style scoped lang="scss">
$margin-bottom: 20px;

.buttonList {
  display: flex;
  justify-content: end;
  padding-bottom: $margin-bottom;
  border-bottom: 1px solid #edeff2;
  margin-bottom: $margin-bottom;
}

.title {
  border-radius: 2px;
  border-left: 5px solid #1890ff;
  height: 28px;
  background: #f7f8f9;
  font-weight: 500;
  font-size: 14px;
  color: #606266;
  padding-left: 12px;
  line-height: 28px;
  // margin-bottom: $margin-bottom;
}
</style>
